package com.attendance.controller;

import com.attendance.common.Result;
import com.attendance.dto.request.RoleRequest;
import com.attendance.entity.Role;
import com.attendance.service.RoleService;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/roles")
public class RoleController {

    @Autowired
    private RoleService roleService;

    @PostMapping
    public Result<Void> createRole(@RequestBody RoleRequest request) {
        roleService.createRole(request);
        return Result.success();
    }

    @PutMapping
    public Result<Void> updateRole(@RequestBody RoleRequest request) {
        roleService.updateRole(request);
        return Result.success();
    }

    @DeleteMapping("/{id}")
    public Result<Void> deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Role> getRole(@PathVariable Long id) {
        return Result.success(roleService.getRole(id));
    }

    @GetMapping
    public Result<Page<Role>> listRoles(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String keyword) {
        return Result.success(roleService.listRoles(pageNum, pageSize, keyword));
    }

    @PutMapping("/{id}/enable")
    public Result<Void> enableRole(@PathVariable Long id) {
        roleService.enableRole(id);
        return Result.success();
    }

    @PutMapping("/{id}/disable")
    public Result<Void> disableRole(@PathVariable Long id) {
        roleService.disableRole(id);
        return Result.success();
    }

    @GetMapping("/enabled")
    public Result<List<Role>> listEnabledRoles() {
        return Result.success(roleService.listEnabledRoles());
    }
} 